package groep3.ssc.controllers;

import groep3.ssc.model.Race;
import groep3.ssc.model.Scheme;
import groep3.ssc.model.SchemeEntry;

import java.util.ArrayList;

import android.util.SparseArray;

public class ModelRepository
{
    private static ModelRepository instance;
    private SparseArray<Race> raceModels;
    private SparseArray<Scheme> schemeModels;
    private DbController database;
    private int currentRaceId;
    private int currentActiveSchemeId;

    private ModelRepository()
    {
        raceModels = new SparseArray<Race>();
        schemeModels = new SparseArray<Scheme>();
        database = DbController.getInstance();
        currentRaceId = -1;
        currentActiveSchemeId = -1;
    }

    public void setCurrentRaceId(int raceId)
    {
        this.currentRaceId = raceId;
    }

    public Race getActiveRace()
    {
        Race r = getRace(currentRaceId);
        if (currentActiveSchemeId != -1 && r != null)
        {
            r.setTargetSchemeId(currentActiveSchemeId);
        }
        return r;
    }

    public Race getRace(int raceId)
    {
        return raceModels.get(raceId);
    }

    public Scheme getScheme(int id)
    {
        Scheme s = schemeModels.get(id);
        if (s == null)
        {
            s = database.getScheme(id);
            schemeModels.put(id, s);
        }
        return schemeModels.get(id);
    }

    public Race getNewRace()
    {
        Race r = new Race();
        int newId = raceModels.size();
        r.setId(newId);
        raceModels.put(newId, r);
        return r;
    }

    public Scheme getNewScheme(String title, String abbreviation)
    {
        Scheme s = new Scheme(title, abbreviation);
        int newId = schemeModels.size();
        s.setId(newId);
        return s;
    }

    public static ModelRepository getInstance()
    {
        if (instance == null)
        {
            instance = new ModelRepository();
        }
        return instance;
    }

    public ArrayList<Scheme> getListOfSchemes()
    {
        ArrayList<Scheme> schemes = new ArrayList<Scheme>();
        ArrayList<Integer> idList = database.getIdListOfSchemes();
        for (Integer i : idList)
        {
            schemes.add(getScheme(i));
        }

        return schemes;
    }

    public void addScheme(Scheme schemeToSave)
    {
        database.addScheme(schemeToSave);
        schemeModels.put(schemeToSave.getId(), schemeToSave);
    }

    public void updateScheme(Scheme scheme)
    {
        getScheme(scheme.getId()).setEntries(scheme.getEntries());
        getScheme(scheme.getId()).setTitle(scheme.getTitle());
        getScheme(scheme.getId()).setAbbreviation(scheme.getAbbreviation());
        updateSchemeEntries(getScheme(scheme.getId()), scheme.getEntries());
        database.updateScheme(scheme);
    }

    private void updateSchemeEntries(Scheme scheme, ArrayList<SchemeEntry> entries)
    {
        calculateTotalTimes(entries);
        scheme.setEntries(entries);
    }

    private void calculateTotalTimes(ArrayList<SchemeEntry> entries)
    {
        long totalTime = 0;
        for (int i = 0; i < entries.size(); i++)
        {
            SchemeEntry currentEntry = entries.get(i);
            totalTime += currentEntry.getLapTime();
            currentEntry.setTotalTime(totalTime);
        }

    }

    public void setActiveSchemeId(int id)
    {
        this.currentActiveSchemeId = id;
    }
}